package com.example.wanbaqian.activity;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.wanbaqian.R;
import com.example.wanbaqian.base.BaseActivity;

/**
 * 欢迎页面
 */
public class SplashActivity extends BaseActivity {

    private ViewPager Splash_ViewPager;
    int[] ints = new int[]{R.drawable.splash_one, R.drawable.splash_two, R.drawable.splash_three};
    boolean misScrolled = false;

    @Override
    protected int getLayout() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setStatusBarColorInFragment();
        Splash_ViewPager = (ViewPager) findViewById(R.id.Splash_ViewPager);
        Splash_ViewPager.setAdapter(new pageAdapter());
        Splash_ViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                switch (state) {
                    case ViewPager.SCROLL_STATE_DRAGGING:
                        misScrolled = false;
                        break;
                    case ViewPager.SCROLL_STATE_SETTLING:
                        misScrolled = true;
                        break;
                    case ViewPager.SCROLL_STATE_IDLE:
                        if (Splash_ViewPager.getCurrentItem() == Splash_ViewPager.getAdapter().getCount() - 1 && !misScrolled) {

                        }
                        misScrolled = true;
                        break;
                }
            }
        });
    }

    @Override
    protected void initDate() {

    }

    class pageAdapter extends PagerAdapter {


        @Override
        public int getCount() {
            return ints.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup view, int position, Object object) {
            view.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_splash, container, false);
            ImageView imageView = (ImageView) view.findViewById(R.id.image);
            imageView.setBackgroundResource(ints[position]);
            container.addView(view);
            if (position == ints.length - 1) {
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        openActivity(LoginActivity.class);
                        finish();
                    }
                });
            }
            return view;
        }
    }
}
